package com.project.api.ym.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.project.api.common.ApiResponse;
import com.project.api.common.Http;
import com.project.api.ym.model.Order;
import com.project.api.ym.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrderController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private final String pf="ym";
    private final String vpf="verify/ym";

    @Autowired
    private OrderService orderService;

    @RequestMapping(value=vpf+"/order_list",method = RequestMethod.GET)
    @ResponseBody
    public String OrderList(HttpServletRequest request, Model model,
                           @RequestParam(value="offset",required = false) @Nullable String  offset,
                           @RequestParam(value="limit",required = false) @Nullable String  limit,
                           @RequestParam(value="title",required = false) @Nullable String  title,
                           @RequestParam(value="atype",required = false) @Nullable String  atype,
                           @RequestParam(value="stime",required = false) @Nullable String  stime,
                           @RequestParam(value="etime",required = false) @Nullable String  etime,
                           @RequestParam(value="status",required = false) @Nullable String  status,
                           @RequestParam(value="sort",required = false) @Nullable String  sort,
                           @RequestParam(value="gtime",required = false) @Nullable String  gtime,
                           @RequestParam(value="otime",required = false) @Nullable String  otime,
                           @RequestParam(value="is_material",required = false) @Nullable String  is_material,
                           @RequestParam(value="is_hot",required = false) @Nullable String  is_hot,
                           @RequestParam(value="is_msg",required = false) @Nullable String  is_msg,
                           @RequestParam(value="user_name",required = false) @Nullable String  user_name,
                           @RequestParam(value="company",required = false) @Nullable String  company,
                           @RequestParam(value="phone",required = false) @Nullable String  phone,
                            @RequestParam(value="order_num",required = false) @Nullable String  order_num,
                            @RequestParam(value="user",required = false) @Nullable String  user,
                            @RequestParam(value="ctime",required = false) @Nullable String  ctime
    ){
        try {
            Map<String , Object> map=new HashMap();
            int o=0;
            int l=20;
            if (offset!=null)o=Integer.parseInt(offset);
            if (limit!=null)l=Integer.parseInt(limit);
            if (title!=null && !title.equalsIgnoreCase("") )map.put("title",title);
            if (order_num!=null && !order_num.equalsIgnoreCase("") )map.put("order_num",order_num);
            if (user!=null && !user.equalsIgnoreCase("") )map.put("user",user);
            if (user_name!=null && !user_name.equalsIgnoreCase(""))map.put("user_name",user_name);
            if (company!=null&& !company.equalsIgnoreCase(""))map.put("company",company);
            if (phone!=null&& !phone.equalsIgnoreCase(""))map.put("phone",phone);
            if (atype!=null&&!atype.equalsIgnoreCase("-1"))map.put("atype",atype);
            if (stime!=null&&!stime.equalsIgnoreCase("-1"))map.put("stime",stime);
            if (etime!=null&&!etime.equalsIgnoreCase("-1"))map.put("etime",etime);
            if (gtime!=null&&!gtime.equalsIgnoreCase("-1"))map.put("gtime",gtime);
            if (otime!=null&&!otime.equalsIgnoreCase("-1"))map.put("otime",otime);
            if (ctime!=null&&!ctime.equalsIgnoreCase("-1"))map.put("ctime",ctime);
            if (is_material!=null&&!is_material.equalsIgnoreCase("-1"))map.put("is_material",is_material);
            if (is_hot!=null&&!is_hot.equalsIgnoreCase("-1"))map.put("is_hot",is_hot);
            if (is_msg!=null&&!is_msg.equalsIgnoreCase("-1"))map.put("is_msg",is_msg);
            if (status!=null&&!status.equalsIgnoreCase("-1"))map.put("status",status);
            List res=orderService.GetOrder(map,o,l,sort);
            int total=orderService.GetOrderCount(map);
            JSONObject json = new JSONObject();
            JSONArray jsonArray = new JSONArray(res);
            json.put("dataList",jsonArray);
            json.put("total",total);
            return ApiResponse.Ok("success",json);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.Fail(e.getMessage());
        }
    }

    @RequestMapping(value=vpf+"/order_one",method = RequestMethod.GET)
    @ResponseBody
    public String OrderOne(@RequestParam(value="id",required = true)  String  id){
        try {
            long i = Long.valueOf(String.valueOf(id));
            Order data = orderService.GetOrderById(i);
            return ApiResponse.Ok("success", data);
        }catch (Exception e){
            return ApiResponse.Fail(e.getMessage());
        }
    }

    @RequestMapping(value=vpf+"/order_refund",method = RequestMethod.POST)
    @ResponseBody
    public String OrderRefund(HttpServletRequest request, Model model, @RequestBody JSONObject json){
        try {
            String order_num=(String) json.getOrDefault("order_num",null);
            Object rprice= json.getOrDefault("refund_price","0");
            double refund_price=Double.valueOf(String.valueOf(rprice));
            Object refund= json.getOrDefault("is_refund","1");
            int is_refund=Integer.valueOf(String.valueOf(refund));
            Object id= json.getOrDefault("id",0);
            String order_id=String.valueOf(id);
            if(refund_price<=0L){
//                throw new Exception("退款金额不能小于0");
            }if(is_refund == 1){
                throw new Exception("已退款的订单不能重复退款");
            }
            MultiValueMap<String,String> map =new LinkedMultiValueMap<>();
            map.add("orderId",order_id);
            String res = Http.sendPostRequest("http://wx.ym0757.com.cn:9003/order/refund?orderId="+order_id,map);
            JSONObject  robj= JSON.parseObject(res);
            int code = (Integer) robj.getOrDefault("code",2233);
            if(code!=0){
                throw new Exception(res);
            }
            long oid= Long.valueOf(order_id);
            Order order = orderService.GetOrderById(oid);
            return ApiResponse.Ok("success", order);
        }catch (Exception e){
            return ApiResponse.Fail(e.getMessage());
        }
    }
}
